package script.db

databaseChangeLog(logicalFilePath: 'script/db/hpfm_cusz_unit_group.groovy') {
    changeSet(author: "hzero@hand-china.com", id: "2022-04-25-hpfm_cusz_unit_group") {
        def weight = 1
        if (helper.isSqlServer()) {
            weight = 2
        } else if (helper.isOracle()) {
            weight = 3
        }
        if (helper.dbType().isSupportSequence()) {
            createSequence(sequenceName: 'hpfm_cusz_unit_group_s', startValue: "1")
        }
        createTable(tableName: "hpfm_cusz_unit_group", remarks: "") {
            column(name: "unit_group_id", type: "bigint", autoIncrement: true, remarks: "") { constraints(primaryKey: true) }
            column(name: "group_code", type: "varchar(" + 255 * weight + ")", remarks: "组编码") { constraints(nullable: "false") }
            column(name: "group_name", type: "varchar(" + 255 * weight + ")", remarks: "组名称") { constraints(nullable: "false") }
            column(name: "group_menu_code", type: "varchar(" + 255 * weight + ")", remarks: "菜单编码") { constraints(nullable: "false") }
            column(name: "object_version_number", type: "bigint", defaultValue: 1, remarks: "行版本号，用来处理锁") { constraints(nullable: "false") }
            column(name: "created_by", type: "bigint", defaultValue: -1, remarks: "")
            column(name: "creation_date", type: "datetime", defaultValueComputed: "CURRENT_TIMESTAMP", remarks: "")
            column(name: "last_updated_by", type: "bigint", defaultValue: -1, remarks: "")
            column(name: "last_update_date", type: "datetime", defaultValueComputed: "CURRENT_TIMESTAMP", remarks: "")
        }

        addUniqueConstraint(columnNames: "group_code", tableName: "hpfm_cusz_unit_group", constraintName: "hpfm_cusz_unit_group_U1")
    }

    changeSet(author: "hzero@hand-china.com", id: "2022-05-26-hpfm_cusz_unit_group") {
        addColumn(tableName: "hpfm_cusz_unit_group") {
            column(name: "tenant_id", type: "bigint", defaultValue: 0, remarks: "租户ID") { constraints(nullable: "false") }
        }
    }

    changeSet(author: 'hzero@hand-china.com', id: '2022-05-26-hpfm_cusz_unit_group-1') {
        dropUniqueConstraint(tableName: "hpfm_cusz_unit_group", constraintName: "hpfm_cusz_unit_group_U1")
    }

    changeSet(author: 'hzero@hand-china.com', id: '2022-05-26-hpfm_cusz_unit_group-2') {
        addUniqueConstraint(tableName: "hpfm_cusz_unit_group", constraintName: "hpfm_cusz_unit_group_u1", columnNames: "group_code, tenant_id")
    }

    changeSet(author: "hzero@hand-china.com", id: "2022-06-24-hpfm_cusz_unit_group") {
        addColumn(tableName: "hpfm_cusz_unit_group") {
            column(name: "menu_tenant_id", type: "bigint", defaultValue: 0, remarks: "菜单租户ID") { constraints(nullable: "false") }
        }
    }

    changeSet(author: "hzero@hand-china.com", id: "2022-06-25-hpfm_cusz_unit_group") {
        def weight = 1
        if (helper.isSqlServer()) {
            weight = 2
        } else if (helper.isOracle()) {
            weight = 3
        }
        addColumn(tableName: "hpfm_cusz_unit_group") {
            column(name: "menu_level", type: "varchar(" + 30 * weight + ")", defaultValue: 'organization', remarks: "菜单层级") { constraints(nullable: "false") }
        }
    }
}